package com.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lxk on 2019/7/13.
 */
public class Solution219 {


    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> numPosMap = new HashMap<>();
        for(int i = 0;i < nums.length; i++){
            if(numPosMap.containsKey(nums[i])){
                Integer pos = numPosMap.get(nums[i]);
                if(Math.abs(pos-i)<=k){
                    return true;
                }else{
                    numPosMap.put(nums[i],i);
                }
            }else{
                numPosMap.put(nums[i],i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,1,2,3};
        Solution219 solution219 = new Solution219();
        System.out.println(solution219.containsNearbyDuplicate(arr,2));
    }

}
